package com.yilijk.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yilijk.dialog.adapter.AdapterBottom;
import com.yilijk.dialog.callback.AlertDialogCallback;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/11/08
 */
public class AlertDialogManager {


    private static  AlertDialogBuilder alertDialogBuilder;

    public static AlertDialogBuilder builder(){
        alertDialogBuilder = new AlertDialogBuilder();
        return alertDialogBuilder;
    }

    public static void dismiss(){
        if (alertDialogBuilder!=null){
            alertDialogBuilder.dismiss();
        }
    }

    public static class AlertDialogBuilder{

        private Context context;
        private AlertDialogConfig config;
        private Dialog dialog;
        private AlertDialogCallback bottom_item_callback;


        private AlertDialogBuilder(){
            config = new AlertDialogConfig();
        }

        /**
         * 当前Activity的上下文
         *
         * @param context
         * @return
         */

        public AlertDialogBuilder setContext(Context context){
            this.context = context;
            return this;
        }


        /**
         * 设置弹出框类型 Type. 弹出各种样式的alertdialog
         * 不设置为默认
         * default: AlertDialogType.ALERT_DIALOG_TYPE_NORMAL
         *
         * @param type
         * @return
         */
        public AlertDialogBuilder setType(int type){
            config.type = type;
            return this;
        }

        /**
         * 设置取消按钮文案
         * default: 否
         *
         * @param negativeButton
         * @return
         */
        public AlertDialogBuilder setNegativeButton(String negativeButton){
            config.NEGATIVE_BUTTON_NAME = negativeButton;
            return this;
        }

        /**
         * 设置确定按钮文案
         * default: 是
         *
         * @param positiveButton
         * @return
         */
        public AlertDialogBuilder setPositiveButton(String positiveButton){
            config.POSITIVE_BUTTON_NAME = positiveButton;
            return this;
        }

        /**
         * 设置标题
         */
        public AlertDialogBuilder setTitle(String title){
            config.title = title;
            return this;
        }
        /**
         * 设置内容
         */
        public AlertDialogBuilder setContent(String content){
            config.content = content;
            return this;
        }
        /**
         * 设置点击回调监听
         *
         * @param callback
         * @return
         */
        public AlertDialogBuilder setBottomItemCallback(AlertDialogCallback callback){
            this.bottom_item_callback = callback;
            return this;
        }

        /**
         * 设置弹出框的列表
         *
         * @param s
         * @return
         */
        public AlertDialogBuilder setBottomLists(String[] s){
            ArrayList<String> strings = new ArrayList<>();
            for (int i = 0; i < s.length; i++) {
                strings.add(s[i]);
            }
            config.listBeans = strings;
            return this;
        }

        /**
         * 设置弹出框的列表
         *
         * @param lists
         * @return
         */
        public AlertDialogBuilder setBottomLists(ArrayList<String> lists){
            config.listBeans = lists;
            return this;
        }


        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public void show(){
            dialog = new Dialog(context, R.style.BottomDialogStyle);
            if (config.type == 1){
                //填充对话框的布局
                View view = LayoutInflater.from(context).inflate(R.layout.alertdialog_bottom, null);
                //初始化控件
                RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.alertdialog_bottom_list);
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                AdapterBottom adapterBottom = new AdapterBottom(R.layout.alertdialog_item_buttom, config.listBeans);
                recyclerView.setAdapter(adapterBottom);
                adapterBottom.notifyDataSetChanged();
                adapterBottom.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        bottom_item_callback.Success(position,config.listBeans.get(position));
                    }
                });
                TextView cancel = (TextView) view.findViewById(R.id.alertdialog_bottom_cancel);
                cancel.setText(config.NEGATIVE_BUTTON_NAME);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });
                //将布局设置给Dialog
                dialog.setContentView(view);
                //获取当前Activity所在的窗体
                Window dialogWindow = dialog.getWindow();
                //设置Dialog从窗体底部弹出
                dialogWindow.setGravity( Gravity.BOTTOM);
                //获得窗体的属性
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = (int) (AlertDialogUtils.getScreenWidth(context)*1);
                lp.y = 20; //设置Dialog距离底部的距离
                dialogWindow.setAttributes(lp); //将属性设置给窗体
            }else if (config.type == 2){
                //填充对话框的布局
                View view = LayoutInflater.from(context).inflate(R.layout.alertdialog_face, null);
                //初始化控件
                TextView title = (TextView) view.findViewById(R.id.dialog_face_title);
                title.setText(config.title);
                TextView content = (TextView) view.findViewById(R.id.dialog_face_content);
//                content.setText(config.content);
                RichText.from(config.content).bind(this)
                        .showBorder(false)
                        .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                        .into(content);
                TextView diaAgree = (TextView) view.findViewById(R.id.dialog_face_disagree);
                diaAgree.setText(config.NEGATIVE_BUTTON_NAME);
                diaAgree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottom_item_callback.ERROR(config.NEGATIVE_BUTTON_NAME);
                    }
                });
                TextView agree = (TextView) view.findViewById(R.id.dialog_face_agree);
                agree.setText(config.POSITIVE_BUTTON_NAME);
                agree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottom_item_callback.Success(config.POSITIVE_BUTTON_NAME);
                    }
                });
                //将布局设置给Dialog
                dialog.setContentView(view);
                //获取当前Activity所在的窗体
                Window dialogWindow = dialog.getWindow();
                //设置Dialog从窗体底部弹出
                dialogWindow.setGravity( Gravity.CENTER);
                //获得窗体的属性
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = (int) (AlertDialogUtils.getScreenWidth(context)*1- UIUtils.dp2px(context,50));
                lp.height = (int) (AlertDialogUtils.getScreenHeight(context)*1- UIUtils.dp2px(context,260));
                dialogWindow.setAttributes(lp); //将属性设置给窗体
            }else if (config.type == 3){
                //填充对话框的布局
                View view = LayoutInflater.from(context).inflate(R.layout.alertdialog_studio, null);
                ((TextView)view.findViewById(R.id.studio_title)).setText(config.title);
                ((TextView)view.findViewById(R.id.studio_content)).setText(config.content);
                view.findViewById(R.id.studio_bt).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottom_item_callback.Success();
                    }
                });
                view.findViewById(R.id.studio_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottom_item_callback.ERROR();
                    }
                });
            }
            dialog.show();//显示对话框
        }

        public void dismiss(){
            if (dialog!=null){
                dialog.dismiss();
            }
        }

    }


}
